package org.aldar.bquest.entity;

import org.aldar.bquest.constants.IConstants;
import org.aldar.bquest.logic.controller.board.transform.TileBoardTransform;
import org.aldar.bquest.util.Logger;
import org.andengine.entity.Entity;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;

/**
 * User: Daria
 * Date: 1/26/14 10:58 PM
 */
public class CrystalTileSprite extends ManaIncreasingTile {

    private static final Logger LOG = new Logger(CrystalTileSprite.class);

    public CrystalTileSprite(int col, int row, ITextureRegion magicTileTextureRegion, VertexBufferObjectManager vertexBufferObjectManager) {
        super(col, row, magicTileTextureRegion, vertexBufferObjectManager);
    }

    public Entity createTileEntity(int col, int row, ITextureRegion magicTileTextureRegion, VertexBufferObjectManager vertexBufferObjectManager) {
        Entity tileContainer = new Entity((int) (TileBoardTransform.getSpriteAdjustedScreenCoords(TileBoardTransform.getScreenCoords(col * CELL_WIDTH, row * CELL_HEIGHT))[0]),
                (int) (TileBoardTransform.getSpriteAdjustedScreenCoords(TileBoardTransform.getScreenCoords(col * CELL_WIDTH, row * CELL_HEIGHT))[1]),
                100, 110) {
            public String toString() {
                return "entity:TileGroundContainer:" + getStyle().toString();
            }
        };

        tileContainer.setAnchorCenter(0, 1);
        AnimatedSprite crystal = new AnimatedSprite(50, 55,
                (ITiledTextureRegion) magicTileTextureRegion,
                vertexBufferObjectManager
        );
        crystal.setAnchorCenter(0.5f, 0.5f);
        crystal.animate(50, getAnimationListener(((ITiledTextureRegion) magicTileTextureRegion).getTileCount()));
        tileContainer.attachChild(crystal);
        setLsSprite(crystal);
        return tileContainer;
    }

/*    @Override
    public void performMatchAction(final int col, final int row) {
        super.performMatchAction(col, row);
        StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
        LOG.i("Crystal Sprite LOG ENTITY MODIFIER in performMatchAction, caller: " + stackTraceElements[3].getClassName() +
                "." + stackTraceElements[3].getMethodName() +
                " line " + stackTraceElements[3].getLineNumber());
        if (temporaryMatchEffect != null) {
            temporaryMatchEffect.run();
            temporaryMatchEffect = null;
        }
        this.getLsSprite().registerEntityModifier(new SequenceEntityModifier(new ScaleAtModifier(0.2f, 1, 3f, 0.5f, 0.5f), new ScaleAtModifier(0.1f, 3f, 1f, 0.5f, 0.5f, new AfterMatchEntityModifierListener(TileBoard.getKey(row, col), this))));

    }
  */

    @Override
    public int getManaAmountForMatch() {
        return IConstants.manaUseForMatch;
    }

    /*
    public AnimatedSprite.IAnimationListener getAnimationListener() {
        return new AnimatedSprite.IAnimationListener() {
            int direction = 1;

            @Override
            public void onAnimationStarted(AnimatedSprite pAnimatedSprite, int pInitialLoopCount) {
            }

            @Override
            public void onAnimationFrameChanged(AnimatedSprite pAnimatedSprite, int pOldFrameIndex, int pNewFrameIndex) {
                if (pNewFrameIndex < pOldFrameIndex) {
                    direction *= -1;
                }
                pAnimatedSprite.setY(pAnimatedSprite.getY() + 0.5f * direction);
            }

            @Override
            public void onAnimationLoopFinished(AnimatedSprite pAnimatedSprite, int pRemainingLoopCount, int pInitialLoopCount) {
            }

            @Override
            public void onAnimationFinished(AnimatedSprite pAnimatedSprite) {
            }
        };
    }
    */
}
